package org.jdemo.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * IOUtil
 *
 * @author longlin(longlin@cyou-inc.com)
 * @date 2013-9-16
 * @since V1.0
 */
public class DateUtil {
	/**
	 * 得到当前的日期字符串，方便存放文件夹
	 * 
	 * @param date 日期对象
	 * @return 返回日期字符串
	 */
	public static final String getDateString(Date date) {
		if(date == null){
			return null;
		}
		SimpleDateFormat formattxt = new SimpleDateFormat("yyyy-MM-dd");
		return formattxt.format(date);
	}

	/**
	 * 得到当前的日期字符串，方便存放文件夹
	 * 
	 * @param date 日期对象
	 * @return 返回日期字符串
	 */
	public static final String getDateChineseString(Date date) {
		if(date == null){
			return null;
		}
		SimpleDateFormat formattxt = new SimpleDateFormat("yyyy年MM月dd日");
		return formattxt.format(date);
	}

	/**
	 * 得到日期/时间字符串
	 * 
	 * @param date 日期对象
	 * @return 返回日期/时间字符串
	 */
	public static final String getDateTimeString(Date date) {
		SimpleDateFormat formattxt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formattxt.format(date);
	}

	/**
	 * 得到当前日期/时间字符串
	 * 
	 * @return 返回日期/时间字符串
	 */
	public static final String getNowDateTimeString() {
		Date date = new Date();
		SimpleDateFormat formattxt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formattxt.format(date);
	}

	/**
	 * 得到当前日期字符串
	 * 
	 * @return 返回当前日期字符串
	 */
	public static final String getNowDateString() {
		Date date = new Date();
		SimpleDateFormat formattxt = new SimpleDateFormat("yyyy-MM-dd");
		return formattxt.format(date);
	}

	public static final Date getDate(int year, int month, int day) {
		return getDate(year + "-" + month + "-" + day + " 00:00:00");
	}
	
	public static final Date getDate(int year, int month, int day, int hour, int miniute, int second) {
		return getDate(year + "-" + month + "-" + day + " " + hour + ":" + miniute + ":" + second);
	}
	
	/**
	 * 得到日期对象
	 * 
	 * @param dateTimeString 日期字符串
	 * @return 返回日期对象
	 */
	public static final Date getDate(String dateTimeString) {
		SimpleDateFormat formattxt = null;
		if(dateTimeString.length() > 10) {
			formattxt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}else {
			formattxt = new SimpleDateFormat("yyyy-MM-dd");
		}
		
		Date date = null;
		try {
			date = formattxt.parse(dateTimeString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 得到上一年的日期字符串
	 * 
	 * @param date 日期对象
	 * @return 上一年的日期字符串
	 */
	public static final String getLastYearString(Date date) {
		Calendar lastYear = Calendar.getInstance();
		lastYear.setTime(date);
		lastYear.add(Calendar.YEAR, -1);
		return DateUtil.getDateString(lastYear.getTime());
	}

	/**
	 * 得到上一月的日期字符串
	 * 
	 * @param date 日期对象
	 * @return 上一月的日期字符串
	 */
	public static final String getLastMonthString(Date date) {
		Calendar lastMonth = Calendar.getInstance();
		lastMonth.setTime(date);
		lastMonth.add(Calendar.MONTH, -1);
		return DateUtil.getDateString(lastMonth.getTime());
	}

	/**
	 * 得到上一周的日期字符串
	 * 
	 * @param date 日期对象
	 * @return 上一月周日期字符串
	 */
	public static final String getLastWeekString(Date date) {
		Calendar lastWeek = Calendar.getInstance();
		lastWeek.setTime(date);
		lastWeek.add(Calendar.HOUR_OF_DAY, -168);
		return DateUtil.getDateString(lastWeek.getTime());
	}

	/**
	 * 得到昨天的日期字符串
	 * 
	 * @param date 日期对象
	 * @return 昨天的日期字符串
	 */
	public static final String getYesterdayString(Date date) {
		Calendar yesterday = Calendar.getInstance();
		yesterday.setTime(date);
		yesterday.add(Calendar.HOUR_OF_DAY, -24);
		return DateUtil.getDateString(yesterday.getTime());
	}
	
	/**
	 * 得到昨天的日期对象
	 * 
	 * @param date 日期对象
	 * @return 昨天的日期字符串
	 */
	public static final Date getYesterdayDate(Date date) {
		Calendar yesterday = Calendar.getInstance();
		yesterday.setTime(date);
		yesterday.add(Calendar.HOUR_OF_DAY, -24);
		return yesterday.getTime();
	}

	/**
	 * 得到当前日期字符串，没有连接线的
	 * 
	 * @return 返回当前日期字符串
	 */
	public static final String getNoLineNowDateString() {
		Date date = new Date();
		SimpleDateFormat formattxt = new SimpleDateFormat("yyyyMMdd");
		return formattxt.format(date);
	}

	/**
	 * 取得当时的时间戳。年（四位）_月（两位）_日（两位）_时（两位）_分（两位）_秒（两位）_毫秒（三位）
	 * 
	 * @return 当时的时间戳
	 */
	public static final String getNowTimeStamp() {
		Date date = new Date();
		SimpleDateFormat formattxt = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
		return formattxt.format(date);

	}

	/**
	 * 取得当时的时间戳。
	 * 
	 * @return 当时的时间戳
	 */
	public static final String getTimeMillis() {
		Date date = new Date();
		SimpleDateFormat formattxt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return formattxt.format(date);
	}

	/**
	 * 返回星期 数字，0周日，1周1，...6周六
	 */
	public static int getWeek() {
		int newWeek = 0;
		String theWeek = "";
		Date date1 = new Date();
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dateStringToParse = bartDateFormat.format(date1);
		// String dateStringToParse = thedate;
		try {
			Date date = bartDateFormat.parse(dateStringToParse);
			SimpleDateFormat bartDateFormat2 = new SimpleDateFormat("EEEE");
			theWeek = bartDateFormat2.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (theWeek.equals("星期日"))
			newWeek = 0;
		else if (theWeek.equals("星期一"))
			newWeek = 1;
		else if (theWeek.equals("星期二"))
			newWeek = 2;
		else if (theWeek.equals("星期三"))
			newWeek = 3;
		else if (theWeek.equals("星期四"))
			newWeek = 4;
		else if (theWeek.equals("星期五"))
			newWeek = 5;
		else if (theWeek.equals("星期六"))
			newWeek = 6;

		return newWeek;
	}
	
	/**
	 * 返回星期中文
	 */
	public static String getWeekCH() {
		//int newWeek = 0;
		String theWeek = "";
		Date date1 = new Date();
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dateStringToParse = bartDateFormat.format(date1);
		// String dateStringToParse = thedate;
		try {
			Date date = bartDateFormat.parse(dateStringToParse);
			SimpleDateFormat bartDateFormat2 = new SimpleDateFormat("EEEE");
			theWeek = bartDateFormat2.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return theWeek;
		
	}
	
	public static final String getDateString(Long time) {
		if(time == null) {
			return "";
		}
		return getDateString(new Date(time));
	}

	public static final String getDateTimeString(Long time) {
		if(time == null) {
			return "";
		}
		return getDateTimeString(new Date(time));
	}
	
	public static final int getYear(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}
	
	public static final int getMonth(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}
	
	public static final int getDay(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	
	public static final int getHour(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR_OF_DAY);
	}
	
	public static boolean isFirstDayOfWeek(){
		boolean b = false;
		 Calendar currentCalendar = Calendar.getInstance();
		 if(currentCalendar.get(Calendar.DAY_OF_WEEK) == 2){
			 b = true;
		 }
		return b;
	}
	
	public static boolean isFirstDayOfMonth(){
		boolean b = false;
		 Calendar currentCalendar = Calendar.getInstance();
		 if(currentCalendar.get(Calendar.DAY_OF_MONTH) == 1){
			 b = true;
		 }
		return b;
	}
	
	public static long getTimeOfDay(Date date) {
		return date.getTime() / (24 * 60 * 60 * 1000);
	}
	
	public static long getTimeOfHour(Date date) {
		return date.getTime() / (60 * 60 * 1000);
	}
	
	public static long getTimeOfMin(Date date) {
		return date.getTime() / (60 * 1000);
	}
	
	public static long getTimeOfSecond(Date date) {
		return date.getTime() / 1000;
	}
	
	/**
	 * 将时间转换成几秒前、几分钟前...
	 * @param date
	 * @return
	 */
	public static String formateDateToMessage(Date date){
		String str = "";
		if(date != null) {
			long internal = ((new Date()).getTime() - date.getTime()) / 1000;
			if(internal < 60) {
				str = internal + " 秒前";
			}else if(internal < 60 * 60) {
				str = internal / 60 + " 分钟前";
			}else if(internal < 60 * 60 * 24) {
				str = internal / (60 * 60) + " 小时前";
			}else if(internal < 60 * 60 * 24 * 3) {
				str = internal / (60 * 60 * 25) + " 天前";
			}else {
				str = getDateString(date);
			}
		}
		return str;
	}
	/**
	 * 
	 * @param currentStr 当前时间 格式yyyy-MM-dd
	 * @param targetStr  目标时间 格式yyyy-MM-dd
	 * @return
	 */
	public static int calcApartDayCount(String currentStr, String targetStr) {
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    Date current = null;
	    Date target = null;
	    try {
	    	current = sdf.parse(currentStr);
	    	target = sdf.parse(targetStr);
		} catch (Exception e) {
			current = new Date();
			target = new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(current);
		long aMillionSeconds = cal.getTimeInMillis();
		cal.setTime(target);
		long bMillionSeconds = cal.getTimeInMillis();
		return (int) ((aMillionSeconds - bMillionSeconds) /(1000*60*60*24));
	}
	
	/**
	 * 
	 * @param current 
	 * @param target  
	 * @return
	 */
	public static int calcApartDayCount(Date current, Date target) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(current);
		long aMillionSeconds = cal.getTimeInMillis();
		cal.setTime(target);
		long bMillionSeconds = cal.getTimeInMillis();
		return (int) ((aMillionSeconds - bMillionSeconds) /(1000*60*60*24));
	}
	
	public static Date addYear(Date date, int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		return calendar.getTime();
	}
	
	public static Date addWeek(Date date, int month) {
		return addDay(date, 7);
	}
	
	public static Date addMonth(Date date, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		return calendar.getTime();
	}
	
	public static Date addDay(Date date, int day) {
		return addHour(date, 24 * day);
	}
	
	public static Date addHour(Date date, long hour) {
		return addMinute(date, 60 * hour);
	}
	
	public static Date addMinute(Date date, long min) {
		return addSecond(date, 60 * min);
	}
	
	public static Date addSecond(Date date, long sec) {
		return addTime(date, sec * 1000);
	}
	
	public static Date subDay(Date date, int day){
		long time = 24 * 60 * 60 * 1000 * day;
		if(date != null) {
			return new Date(date.getTime() - time);
		}
		return null;
	}
	
	public static Date addTime(Date date, long time) {
		if(date != null) {
			return new Date(date.getTime() + time);
		}
		return null;
	}
	
	public static boolean isTheSameDay(Date d1,Date d2) {  
        Calendar c1 = Calendar.getInstance();  
        Calendar c2 = Calendar.getInstance();  
        c1.setTime(d1);  
        c2.setTime(d2);  
        return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))  
                && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))  
                && (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH));  
    }
	
	public static boolean isTheSameMonth(Date d1,Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);  
        return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
                && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH));
    }
	
	//返回某一个月的天数
	public static int getDayOfMonth(Date date) {
		return getDayOfMonth(getYear(date), getMonth(date));
	}

	//返回某一个月的天数
	public static int getDayOfMonth(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	public static long getDayTimeOfNow() {
		return new Date().getTime() / (1000 * 60 * 60 * 24);
	}
	
	//一天的开始
	public static Date getBeginTimeOfDay(Date date) {
		int year = getYear(date);
		int month = getMonth(date);
		int day = getDay(date);
		return getDate(year + "-" + month + "-" + day + " 00:00:00");
	}
	
	//一天的结束
	public static Date getEndTimeOfDay(Date date) {
		int year = getYear(date);
		int month = getMonth(date);
		int day = getDay(date);
		return getDate(year + "-" + month + "-" + day + " 23:59:59");
	}
	
	//一月的开始
	public static Date getBeginTimeOfMonth(Date date) {
		int year = getYear(date);
		int month = getMonth(date);
		return getDate(year + "-" + month + "-01 00:00:00");
	}
	
	//一月的结束
	public static Date getEndTimeOfMonth(Date date) {
		int year = getYear(date);
		int month = getMonth(date);
		return getDate(year + "-" + month + "-" + getDayOfMonth(year, month) + " 23:59:59");
	}
	
	//一年的开始
	public static Date getBeginTimeOfYear(Date date) {
		int year = getYear(date);
		return getDate(year + "-01-01 00:00:00");
	}
	
	//一年的结束
	public static Date getEndTimeOfYear(Date date) {
		int year = getYear(date);
		return getDate(year + "-12-31 23:59:59");
	}
	
	public static void main(String[] args) {
        System.out.println(getDateTimeString(1381219603373L));
    }
}

